package com.zy.fee.roadpark.activity.np;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.adapter.EscapeItemAdapter;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.dao.NpNodeRecordLogic;
import com.zy.fee.roadpark.model.M_EscapeItem;
import com.zy.fee.roadpark.model.apimodel.APIM_EscapeItemList;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.ColorUtil;
import com.zygk.library.util.Convert;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.RxTextTool;
import com.zygk.library.util.ToastUtil;
import com.zygk.library.view.SmoothListView.SmoothListView;
import java.util.ArrayList;
import java.util.List;

/**
 * 补缴单据列表
 * Created by qhj on 2018/9/13 0013.
 */

public class NpOverpayBillListActivity extends BaseActivity
    implements SmoothListView.ISmoothListViewListener {
  public static final String INTENT_PLATE_NUMBER = "INTENT_PLATE_NUMBER";

  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.tv_money) TextView tvMoney;
  @BindView(R.id.smoothListView) SmoothListView mSmoothListView;
  @BindView(R.id.rl_no_data) RelativeLayout rlNoData;
  @BindView(R.id.tv_plate_number) TextView tvPlateNumber;

  private EscapeItemAdapter adapter;
  private int page = 0;
  private String plateNumber;
  private List<M_EscapeItem> mEscapeItemList;

  @Override public void setContentView() {
    setContentView(R.layout.activity_np_overpay_bill);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    plateNumber = getIntent().getStringExtra(INTENT_PLATE_NUMBER);
    mEscapeItemList = new ArrayList<>();
    adapter = new EscapeItemAdapter(mContext, mEscapeItemList);
    mSmoothListView.setAdapter(adapter);

    registerReceiver(new String[] { Constants.BROADCAST_REFRESH_OVERPAY_BILL_LIST });
  }

  private void initView() {
    lhTvTitle.setText("补缴单据列表");
    tvPlateNumber.setText("车牌号：" + plateNumber);

    mSmoothListView.setSmoothListViewListener(this);
    mSmoothListView.setRefreshEnable(true);
    mSmoothListView.setLoadMoreEnable(false);

    getEscapeList(false);
  }

  private void initListener() {
  }

  private void getEscapeList(final boolean isLoadMore) {
    NpNodeRecordLogic.npnode_getCarEscapeList(mContext, plateNumber,
        isLoadMore ? ++page : (page = 1), Constants.ROWS, new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showPd();
          }

          @Override public void onSucceed(Object obj) {
            APIM_EscapeItemList result = (APIM_EscapeItemList) obj;
            RxTextTool.getBuilder("", mContext)
                .append("欠缴总额：")
                .setBold()
                .append(Convert.getMoneyString(result.getTotalMoney()))
                .setBold()
                .setForegroundColor(ColorUtil.getColor(mContext, R.color.theme_red))
                .append("元")
                .setBold()
                .into(tvMoney);
            mEscapeItemList= result.getEscapeList();
            fillAdapter(mEscapeItemList, result.getMaxpage(), isLoadMore);
          }

          @Override public void onFailed() {
            ToastUtil.showNetErrorMessage(mContext);
          }

          @Override public void onFinish() {
            dismissPd();
            mSmoothListView.stopRefresh();
            mSmoothListView.stopLoadMore();
          }
        });
  }

  // 填充数据
  private void fillAdapter(List<M_EscapeItem> list, int maxPage, boolean isLoadMore) {
    if (list == null || list.size() == 0) {
      rlNoData.setVisibility(View.VISIBLE);
      mSmoothListView.setVisibility(View.GONE);
      mSmoothListView.setLoadMoreEnable(false);
    } else {
      rlNoData.setVisibility(View.GONE);
      mSmoothListView.setVisibility(View.VISIBLE);
      mSmoothListView.setLoadMoreEnable(page < maxPage);
      adapter.setData(list, isLoadMore);
    }
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override protected void handleReceiver(Context context, Intent intent) {
    super.handleReceiver(context, intent);
    if (intent == null || TextUtils.isEmpty(intent.getAction())) {
      return;
    }
    if (Constants.BROADCAST_REFRESH_OVERPAY_BILL_LIST.equals(intent.getAction())) {
      getEscapeList(false);
    }
  }

  @OnClick(R.id.ll_back) public void onViewClicked() {
    finish();
  }

  @Override public void onRefresh() {
    getEscapeList(false);
  }

  @Override public void onLoadMore() {
    getEscapeList(true);
  }
}
